#!/bin/sh
#to make executable chmod 755 RunTests.sh
LATTE=./../lat-compiler/latte
globallog=lattakia_tests.log #output log file
rm -f $globallog
error=0
Check() {
    echo "Testing Latte File $1"

    basename=`echo $1 | sed  s/.lat//`
    reffile=${basename}.txt
    outfile=${basename}.out
    difffile=${basename}.diff
    
    echo "--------------------------------" 1>&2
    echo "Starting test" 1>&2
    echo "Base File Name:  $basename " 1>&2
    echo "Truth File:  $reffile" 1>&2
    echo "Output File: $outfile"  1>&2
    echo "Diff File:  $difffile"  1>&2 

    $LATTE < $1 > $outfile 
    $LATTE < $1 1>&2
    $LATTE < $1 > $outfile || {      
	echo "FAILED: latte terminated"
	echo "--->FAILED: latte terminated - DUMPING" 1>&2
	error=1 ; return 0
    }
    diff -b $reffile $outfile > $difffile 1>&2 || {
	echo "FAILED: output mismatch"
	echo "--->FAILED: DIFF - DUMPING" 1>&2
	diff -b $reffile $outfile 1>&2
	diff -b $reffile $outfile > $difffile

	error=1 ; return 0
    }
    rm $outfile $difffile
    echo OK
}

for file in ./*.lat
do
    Check $file 2>> $globallog
done
exit $error

